package com.prs.crm.service.cdxh.impl;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.prs.crm.dao.cdxh.SaleOrderDao;
import com.prs.crm.domain.cdxh.DistributionOrder;
import com.prs.crm.domain.cdxh.DistributionOrderDetail;
import com.prs.crm.domain.cdxh.MaterialProduct;
import com.prs.crm.domain.cdxh.SaleOrder;
import com.prs.crm.domain.cdxh.SaleOrderDetail;
import com.prs.crm.domain.cdxh.SerialNumberss;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.domain.cdxh.distribution.DistributionReturn;
import com.prs.crm.domain.cdxh.distribution.DistributionReturnDetail;
import com.prs.crm.domain.cdxh.material.MaterialTeam;
import com.prs.crm.domain.cdxh.molding.Molding;
import com.prs.crm.domain.cdxh.molding.MoldingTeam;
import com.prs.crm.domain.cdxh.printing.Printing;
import com.prs.crm.domain.cdxh.printing.PrintingTeam;
import com.prs.crm.service.cdxh.DistributionOrderDetailService;
import com.prs.crm.service.cdxh.DistributionOrderService;
import com.prs.crm.service.cdxh.DistributionReturnDetailService;
import com.prs.crm.service.cdxh.DistributionReturnService;
import com.prs.crm.service.cdxh.MaterialProductService;
import com.prs.crm.service.cdxh.MaterialTeamService;
import com.prs.crm.service.cdxh.MoldingService;
import com.prs.crm.service.cdxh.MoldingTeamService;
import com.prs.crm.service.cdxh.PrintingService;
import com.prs.crm.service.cdxh.PrintingTeamService;
import com.prs.crm.service.cdxh.SaleOrderService;
import com.prs.crm.service.cdxh.SerialNumberssService;
import com.prs.crm.service.cdxh.cd_ProductService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class SaleOrderServiceImpl extends
		BaseServiceImpl<SaleOrder, SaleOrderDao> implements SaleOrderService {
	private DistributionOrder distributionOrder;
	private DistributionOrderService distributionOrderService;
	private SerialNumberssService serialNumberssService;
	private MaterialProductService materialProductService;
	private SaleOrderService saleOrderService;
	private MaterialTeamService materialTeamService;
	private cd_ProductService cd_ProductService;
	private MoldingService moldingService;
	private MoldingTeamService moldingTeamService;
	private DistributionOrderDetailService distributionOrderDetailService;
	private PrintingService printingService;
	private PrintingTeamService printingTeamService;
	private DistributionReturnService distributionReturnService;
	private DistributionReturnDetailService distributionReturnDetailService;

	public String getsaleNum() {
		String saleNums = "03040020101202X1";
		SerialNumberss snObject = this.getSerialNumberssService()
				.getSerialNumberss("销售订单");
		if (snObject == null) {
			SerialNumberss snmm = new SerialNumberss();
			snmm.setStyCategory("销售订单");
			snmm.setStyNumberCode(saleNums);
			this.getSerialNumberssService().save(snmm);
		} else {
			String codeF = snObject.getStyNumberCode();
			String DF[] = codeF.split("X");
			int lastCode = Integer.parseInt(DF[1]) + 1;
			saleNums = DF[0] + "X" + lastCode;
		}
		return saleNums;
	}

	@Override
	public void saveRegister(SaleOrder saleCustomer, String[] productCodes,
			String[] pronames, String[] productNorms, String[] productColors,
			String[] productpackges, String[] productNums,
			String[] productUnits, String[] poductPrices,
			String[] productSquares, String[] prototalPrices) {
		/**
		 * 订单编号
		 */
		String saleNums = "03040020101202X1";
		SerialNumberss snObject = this.getSerialNumberssService()
				.getSerialNumberss("销售订单");
		if (snObject == null) {
			SerialNumberss snmm = new SerialNumberss();
			snmm.setStyCategory("销售订单");
			snmm.setStyNumberCode(saleNums);
			this.getSerialNumberssService().save(snmm);
		} else {
			String codeF = snObject.getStyNumberCode();
			String DF[] = codeF.split("X");
			int lastCode = Integer.parseInt(DF[1]) + 1;
			saleNums = DF[0] + "X" + lastCode;
			snObject.setStyNumberCode(saleNums);

			this.getSerialNumberssService().setNumCode(snObject);
		}

		int lengths = productCodes.length;
		float totalPrice = 0.000f;
		Set<SaleOrderDetail> details = new HashSet<SaleOrderDetail>();
		for (int i = 0; i < lengths; i++) {
			SaleOrderDetail sale = new SaleOrderDetail();
			sale.setProductCode(productCodes[i]);
			sale.setProductName(pronames[i]);
			sale.setProductNorm(productNorms[i]);
			sale.setProductColor(productColors[i]);
			sale.setProductPaceStyle(productpackges[i]);
			sale.setProductNum(Integer.parseInt(productNums[i]));
			sale.setPriceUnit(Double.parseDouble(poductPrices[i]));
			sale.setProductUnit(productUnits[i]);
			sale.setSquare(productSquares[i]);
			sale.setSubtotal(Double.parseDouble(prototalPrices[i]));
			sale.setProDefective(0);
			totalPrice = totalPrice + Float.parseFloat(prototalPrices[i]);
			sale.setSalesCodeN(saleNums);
			sale.setSaleOrder(saleCustomer);
			details.add(sale);
		}
		
		saleCustomer.setDetails(details);
		saleCustomer.setSaleOrderCode(saleNums);
		saleCustomer.setTotalPrice(totalPrice);
		super.save(saleCustomer);
		/**
		 * 添加到配送单里面
		 */
		DistributionOrder disObject = new DistributionOrder();
		for (int j = 0; j < lengths; j++) {
			DistributionOrderDetail dbObject = new DistributionOrderDetail();
			dbObject.setProductCode(productCodes[j]);
			dbObject.setProductName(pronames[j]);
			dbObject.setProductNorm(productNorms[j]);
			dbObject.setProductPackWay(productpackges[j]);
			dbObject.setDisNum(Integer.parseInt(productNums[j]));
			dbObject.setProToNums(0);
			dbObject.setProductColor(productColors[j]);
			// 平方
			String pfs[] = productNorms[j].split("\\*");
			double pf = Double.parseDouble(pfs[1])
					* Double.parseDouble(productNums[j]);
			dbObject.setDisSquare(pf);
			dbObject.setDisPrice(Double.parseDouble(poductPrices[j]));
			dbObject.setDisTotal(Double.parseDouble(prototalPrices[j]));
			dbObject.setSaleOrderPlan(saleNums);
			disObject.addDetail(dbObject);

		}
		disObject.setDisTotalMoney(0.0);
		disObject.setDisOrderCode(saleNums);
		disObject.setDisOrderCustomer(saleCustomer.getCusName());
		disObject.setState("7");
		this.getDistributionOrderService().save(disObject);

		/**
		 * 加入配料单据里面
		 */

		for (int i = 0; i < lengths; i++) {

			MaterialProduct mpObject = new MaterialProduct();
			mpObject.setProductCode(productCodes[i]);
			mpObject.setProductName(pronames[i]);
			mpObject.setProductColor(productColors[i]);
			mpObject.setProductNorm(productNorms[i]);
			mpObject.setDeliveryDate(saleCustomer.getSaleDate());
			mpObject.setProductNum(Integer.parseInt(productNums[i]));
			mpObject.setPlanOrOrderId(saleNums);
			mpObject.setProductPackstyle(productpackges[i]);
			mpObject.setRushOrder("否");
			mpObject.setBackMakeState(1);
			mpObject.setpLState(1);
			mpObject.setAlreadyProduct(0.000);
			mpObject.setFirstYN(1);
			mpObject.setRemainderNum(0.000);
			mpObject.setProductPackstyle(productpackges[i]);
			mpObject.setBackMakeState(1);
			mpObject.setNumConfirmState(2);
			mpObject.setNeedNum(0.000);
			
			this.getMaterialProductService().save(mpObject);
		}

	}

	public SerialNumberssService getSerialNumberssService() {
		return serialNumberssService;
	}

	public void setSerialNumberssService(
			SerialNumberssService serialNumberssService) {
		this.serialNumberssService = serialNumberssService;
	}

	public DistributionOrder getDistributionOrder() {
		return distributionOrder;
	}

	public void setDistributionOrder(DistributionOrder distributionOrder) {
		this.distributionOrder = distributionOrder;
	}

	public DistributionOrderService getDistributionOrderService() {
		return distributionOrderService;
	}

	public void setDistributionOrderService(
			DistributionOrderService distributionOrderService) {
		this.distributionOrderService = distributionOrderService;
	}

	public MaterialProductService getMaterialProductService() {
		return materialProductService;
	}

	public void setMaterialProductService(
			MaterialProductService materialProductService) {
		this.materialProductService = materialProductService;
	}

	/**
	 * 改变和查询生产部分的状态
	 */
	@Override
	public void setAllRushOrder(SaleOrder saleOrder) {
		String orderplan = saleOrder.getSaleOrderCode();
		String runshs = saleOrder.getRushOrder();

		/**
		 * 配料
		 */
		List<MaterialProduct> objectA = this.getMaterialProductService()
				.getMaterialProductOrderpLan(orderplan);

		int alengths = objectA.size();
		if (alengths != 0) {
			for (MaterialProduct aa : objectA) {
				aa.setRushOrder(runshs);
				this.getMaterialProductService().saveOrMaterialProduct(aa);
			}
		}

		List<MaterialTeam> objectB = this.getMaterialTeamService()
				.getMaterialTeamOrderpLan(orderplan);

		int blengths = objectB.size();
		if (blengths != 0) {
			for (MaterialTeam bb : objectB) {
				bb.setMaterialRushOrder(runshs);
				this.getMaterialTeamService().saveOrMaterialTeam(bb);
			}
		}
		/**
		 * 成型
		 */
		List<Molding> objectC = this.getMoldingService().getMoldingOrderpLan(
				orderplan);

		int clengths = objectC.size();
		if (clengths != 0) {
			for (Molding cc : objectC) {
				cc.setMoldingRushOrder(runshs);
				this.getMoldingService().saveOrMolding(cc);
			}
		}
		List<MoldingTeam> objectD = this.getMoldingTeamService()
				.getMoldingTeamOrderpLan(orderplan);

		int dlengths = objectD.size();
		if (dlengths != 0) {
			for (MoldingTeam dd : objectD) {
				dd.setRunshOrder(runshs);
				this.getMoldingTeamService().saveOrMoldingTeam(dd);
			}
		}
		/**
		 * 印刷
		 */
		List<Printing> objectE = this.getPrintingService()
				.getPrintingOrderpLan(orderplan);

		int Elengths = objectE.size();
		if (Elengths != 0) {
			for (Printing ee : objectE) {
				ee.setPrintRushOrder(runshs);
				this.getPrintingService().saveOrPrinting(ee);
			}
		}
		List<PrintingTeam> objectF = this.getPrintingTeamService()
				.getPrintingTeamOrderpLan(orderplan);

		int flengths = objectF.size();
		if (flengths != 0) {
			for (PrintingTeam ff : objectF) {
				ff.setPrintRushOrser(runshs);
				this.getPrintingTeamService().saveOrPrintingTeam(ff);
			}
		}
	}

	@Override
	public SaleOrder getSaleOrder(Serializable id) {
		// TODO Auto-generated method stub
		return this.getDao().getSaleOrder(id);
	}

	@Override
	public String setSaleState(String plancode, String proCode) {

		String stats = "未进入生产";
		/**
		 * 配料
		 */
	
		MaterialProduct objectA = this.getMaterialProductService()
				.getMaterialProductOrPro(plancode, proCode);

		if (objectA != null) {
			stats = "配料生产中";
		}
		MaterialTeam objectB = this.getMaterialTeamService().getMaterialTeam(
				proCode, plancode);

		if (objectB != null) {
			stats = "成型生产中";
		}
		/**
		 * 成型
		 */
	
		Molding objectC = this.getMoldingService()
				.getMolding(plancode, proCode);

		if (objectC != null) {
			stats = "成型生产中";
		}

		MoldingTeam objectD = this.getMoldingTeamService().getMoldingTeam(
				proCode, plancode);

		if (objectD != null) {
			cd_Product cdObject = this.getCd_ProductService().getProductObject(
					proCode);
			if (cdObject != null) {
				int ds = cdObject.getPrintName();
				if (ds == 2) {
					stats = "已完成生产";
				} else {
					stats = "印刷生产中";
				}
			}

		}
		/**
		 * 印刷
		 */
		
		Printing objectE = this.getPrintingService().getPrinting(proCode,
				plancode);

		if (objectE != null) {
			stats = "印刷生产中";
		}
		PrintingTeam objectF = this.getPrintingTeamService().getPrintingTeam(
				plancode, proCode);

		if (objectF != null) {
			stats = "已完成生产";
		}
		return stats;
	}

	public MaterialTeamService getMaterialTeamService() {
		return materialTeamService;
	}

	public void setMaterialTeamService(MaterialTeamService materialTeamService) {
		this.materialTeamService = materialTeamService;
	}

	public MoldingService getMoldingService() {
		return moldingService;
	}

	public void setMoldingService(MoldingService moldingService) {
		this.moldingService = moldingService;
	}

	public MoldingTeamService getMoldingTeamService() {
		return moldingTeamService;
	}

	public void setMoldingTeamService(MoldingTeamService moldingTeamService) {
		this.moldingTeamService = moldingTeamService;
	}

	public PrintingService getPrintingService() {
		return printingService;
	}

	public void setPrintingService(PrintingService printingService) {
		this.printingService = printingService;
	}

	public PrintingTeamService getPrintingTeamService() {
		return printingTeamService;
	}

	public void setPrintingTeamService(PrintingTeamService printingTeamService) {
		this.printingTeamService = printingTeamService;
	}

	public cd_ProductService getCd_ProductService() {
		return cd_ProductService;
	}

	public void setCd_ProductService(cd_ProductService cd_ProductService) {
		this.cd_ProductService = cd_ProductService;
	}

	@Override
	public void setProPlanNums(SaleOrder saleOrder, String[] nums,
			String[] productCodes) {

		/**
		 * 修改销售订单
		 */
		int inSize = productCodes.length;
		double tolY = 0.0;
		if (inSize != 0) {
			for (int i = 0; i < inSize; i++) {
				SaleOrderDetail sod = this.getSaleOrderDetail(productCodes[i],
						saleOrder);
				if (sod != null) {
					sod.setProductNum(Integer.parseInt(nums[i]));
					sod.setSquare(calculateSquar(sod.getProductNorm(), nums[i]));
					sod.setSubtotal(calculatePrice(sod.getPriceUnit(), nums[i]));
					tolY = tolY + calculatePrice(sod.getPriceUnit(), nums[i]);
					this.getSaleOrderService().saveOrudetail(sod);
				}
			}
		}
		saleOrder.setTotalPrice((float) tolY);
		saleOrder.setProPlanNums(6);
		this.getSaleOrderService().saveOrudsale(saleOrder);
		/**
		 * 配料组
		 */
		for (int x = 0; x < inSize; x++) {
			MaterialProduct sd = this.getMaterialProductService()
					.getMaterialProductOrPro(saleOrder.getSaleOrderCode(),
							productCodes[x]);
			if (sd != null) {
				sd.setProductNum(Integer.parseInt(nums[x]));
				sd.setNumConfirmState(1);
				this.getMaterialProductService().saveOrMaterialProduct(sd);

			}
		}
		/**
		 * 配送单
		 */
		for (int t = 0; t < inSize; t++) {
			DistributionOrderDetail dod = this
					.getDistributionOrderDetailService()
					.setDistributionOrderDetail(saleOrder.getSaleOrderCode(),
							productCodes[t]);
			dod.setDisNum(Integer.parseInt(nums[t]));
			dod.setDisSquare(Double.parseDouble(calculateSquar(dod.getProductNorm(), nums[t])));
			dod.setDisTotal(calculatePrice(dod.getDisPrice(), nums[t]));
			this.getDistributionOrderDetailService().submit(dod);
		}
		

	}

	@Override
	public SaleOrderDetail getSaleOrderDetail(String productCode,
			SaleOrder aleOrder) {
		return this.getDao().getSaleOrderDetail(productCode, aleOrder);
	}

	/**
	 * 计算平方
	 * 
	 * @param norm
	 * @param nums
	 * @return
	 */
	public String calculateSquar(String norm, String nums) {
		String[] norms = norm.split("\\*");
		String ss = ((Integer.parseInt(nums) * Integer.parseInt(norms[1])) / 1000)
				+ "";
		return ss;
	}

	/**
	 * 计算小计
	 * 
	 * @param priceUnit
	 * @param nums
	 * @return
	 */
	public Double calculatePrice(Double priceUnit, String nums) {
		double ss = priceUnit * Double.parseDouble(nums);
		return ss;
	}

	@Override
	public void saveOrudetail(SaleOrderDetail saleOrderDetail) {
		this.getDao().saveOrudetail(saleOrderDetail);

	}

	@Override
	public void saveOrudsale(SaleOrder saleOrder) {
		this.getDao().saveOrudsale(saleOrder);
	}

	public SaleOrderService getSaleOrderService() {
		return saleOrderService;
	}

	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}

	public DistributionOrderDetailService getDistributionOrderDetailService() {
		return distributionOrderDetailService;
	}

	public void setDistributionOrderDetailService(
			DistributionOrderDetailService distributionOrderDetailService) {
		this.distributionOrderDetailService = distributionOrderDetailService;
	}

	@Override
	public SaleOrder getSaleOrder(String orders) {
		// TODO Auto-generated method stub
		return this.getDao().getSaleOrder(orders);
	}

	@Override
	public void setSaleOrders(Integer[] ids) {
		String orderCodes=""; 
		//销售订单删除处理
		for(Integer id:ids){
		SaleOrder  soObject	=this.getSaleOrderService().getSaleOrder(id);
         orderCodes=soObject.getSaleOrderCode();
		List<SaleOrderDetail> aklist=this.getSaleOrderService().getSaleOrderDetails(soObject);
		for(SaleOrderDetail sd:aklist){
		this.getSaleOrderService().removeSaleOrderDetail(sd);	
		}
		this.getSaleOrderService().removeSaleOrder(soObject);	
		
		//生产部分删除
		/**
		 * 配料
		 */
		List<MaterialProduct>  mpList=this.getMaterialProductService().getMaterialProductOrderpLan(orderCodes);
		if(mpList.size()>0){
		for(MaterialProduct mp:mpList){
		this.getMaterialProductService().remove(mp);	
		   }		
		}
		List<MaterialTeam>     mtList=this.getMaterialTeamService().getMaterialTeamOrderpLan(orderCodes);
		if(mtList.size()>0){
			for(MaterialTeam mp:mtList){
			this.getMaterialTeamService().remove(mp);	
			   }		
			}
		/**
		 * 成型
		 */
		List<Molding> mdList=this.getMoldingService().getMoldingOrderpLan(orderCodes);
		if(mdList.size()>0){
			for(Molding m:mdList){
				this.getMoldingService().remove(m);				
			}		
		}
		List<MoldingTeam> mtdList=this.getMoldingTeamService().getMoldingTeamOrderpLan(orderCodes);
		if(mtdList.size()>0){
			for(MoldingTeam mte:mtdList){
				this.getMoldingTeamService().remove(mte);
			}
		}
		/**
		 * 印刷
		 */
		List<Printing> pList=this.getPrintingService().getPrintingOrderpLan(orderCodes);
		if(pList.size()>0){
			for(Printing p:pList){
				this.getPrintingService().remove(p);
			}
		}
		List<PrintingTeam> ptList=this.getPrintingTeamService().getPrintingTeamOrderpLan(orderCodes);
		if(ptList.size()>0){
		for(PrintingTeam pt:ptList){
		this.getPrintingTeamService().remove(pt);	
		}	
		}
		//配送部门删除
		DistributionOrder disOBject=this.getDistributionOrderService().setDistributionOrder(orderCodes);
		List<DistributionOrderDetail> ddObject=this.getDistributionOrderDetailService().getDistributionOrderDetails(disOBject);
		if(ddObject.size()>0){
		for(DistributionOrderDetail dod:ddObject){
			this.getDistributionOrderDetailService().remove(dod);
		}
		}
		this.getDistributionOrderService().remove(disOBject);
		//打回部分删除
	   List<DistributionReturn>  drList=this.getDistributionReturnService().getDistributionReturn(orderCodes);
	   for(DistributionReturn  df:drList){
		List<DistributionReturnDetail>  drdList=this.getDistributionReturnDetailService().getDistributionReturnDetails(df); 
		if(drdList.size()>0){ 
		for(DistributionReturnDetail dbr:drdList){
			this.getDistributionReturnDetailService().remove(dbr);
			  
		  } 
		}
		this.getDistributionReturnService().remove(df);
	   }
	    
		}
	}

	@Override
	public void removeSaleOrder(SaleOrder so) {
		this.getDao().removeSaleOrder(so);
	}

	@Override
	public void removeSaleOrderDetail(SaleOrderDetail sod) {
		this.getDao().removeSaleOrderDetail(sod);
	}

	@Override
	public List<SaleOrderDetail> getSaleOrderDetails(SaleOrder saleOrders) {
		// TODO Auto-generated method stub
		return this.getDao().getSaleOrderDetails(saleOrders);
	}

	public DistributionReturnService getDistributionReturnService() {
		return distributionReturnService;
	}

	public void setDistributionReturnService(
			DistributionReturnService distributionReturnService) {
		this.distributionReturnService = distributionReturnService;
	}

	public DistributionReturnDetailService getDistributionReturnDetailService() {
		return distributionReturnDetailService;
	}

	public void setDistributionReturnDetailService(
			DistributionReturnDetailService distributionReturnDetailService) {
		this.distributionReturnDetailService = distributionReturnDetailService;
	}

	@Override
	public SaleOrderDetail getSaleOrderDetailByproductCode(String productCode) {
		// TODO Auto-generated method stub
		return this.getDao().getSaleOrderDetail(productCode);
	}

	@Override
	public SaleOrderDetail getSaleOrderDetailByProduct(cd_Product product) {
		// TODO Auto-generated method stub
		return this.getDao().getSaleOrderDetailByProduct(product);
	}

}
